from django.db import models

class User(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='user id')
    username = models.CharField(max_length=30, unique=True, verbose_name='user name')
    password = models.CharField(max_length=30, verbose_name='password')
    token = models.CharField(max_length=50, verbose_name='user token', default="")
    profile = models.CharField(max_length=100, verbose_name='个人简介', default="")

    REQUIRED_FIELDS = ["password"]
    USERNAME_FIELD = "username"

    class AuthLevel(models.TextChoices):
        NORMAL = 'normal', 'Normal'
        ADMIN = 'admin', 'Admin'
        ROOT = 'root', 'Root'

    auth = models.CharField(
        max_length=10,
        choices=AuthLevel.choices,
        default=AuthLevel.NORMAL,
        verbose_name='auth'
    )

    def to_dict(self) -> dict:
        return {
            'id': self.id,
            'username': self.username,
            'profile': self.profile,
            'auth': self.auth
        }

    def isAdmin(self):
        return self.auth == self.AuthLevel.ADMIN or self.auth == self.AuthLevel.ROOT

    def __str__(self):
        return self.username

    @property
    def is_anonymous(self):
        """
        Always return False. This is a way of comparing User objects to
        anonymous users.
        """
        return False

    @property
    def is_authenticated(self):
        """
        Always return True. This is a way to tell if the user has been
        authenticated in templates.
        """
        return True

    class Meta:
        db_table = 'user'
